package cn.itcast.customview.View;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Scroller;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/*
* 侧滑菜单
* * */
public class SlideLayout extends FrameLayout {

    private View ContentView;
    private View MenuView;
    /*
    * Content的宽
    * */
    private int ContentWidth;
    private int MeunWidth;

    private int ViewHeidth;//他们的高都是相同的

    //滚动者
    private Scroller scroller;






    public SlideLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        scroller=new Scroller(context);
    }

    /*
    * 当布局文件加载完成的时候回调这个方法
    * */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        ContentView=getChildAt(0);
        MenuView=getChildAt(1);


    }
/*
*
* 在测量方法里，得到各个控件的宽和高
* */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        ContentWidth=ContentView.getMeasuredWidth();
        MeunWidth=MenuView.getMeasuredWidth();
        ViewHeidth=getMeasuredHeight();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        //指定菜单的位置
        MenuView.layout(ContentWidth,0,ContentWidth+MeunWidth,ViewHeidth);
    }

    private float startX;
    private float startY;
    private float downX;//并赋值一次
    private float downY;


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                //1.按下记录坐标
                downX=startX=event.getX();
                downY=startY=event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                //2.记录结束时
                float endX=event.getX();
                float endY=event.getY();
                //3.计算偏移量
                float distanceX=endX-startX;

                int toScrollX=(int)(getScrollX()-distanceX);


                //4.屏蔽非法值
                if (toScrollX<0){
                    toScrollX=0;

                }else if (toScrollX>MeunWidth){
                    toScrollX=MeunWidth;
                }
                scrollTo(toScrollX,getScrollY());
                startX=event.getX();
                startY=event.getY();

                //在X轴和Y轴移动的距离
                float DX=Math.abs(endX-downX);
                float DY=Math.abs(endY-downY);
                if (DX>DY&&DX>8){
                    //水平方向滑动
                    //响应侧滑
                    //反拦截-事件给Slidelayout
                    getParent().requestDisallowInterceptTouchEvent(true);

                }

                break;
            case MotionEvent.ACTION_UP:
                int totalScrollX=getScrollX();//偏移量
                if (totalScrollX<MeunWidth/2){
                    //关闭Menu
                    cloneMenu();
                }else{
                    //打开Menu
                    openMenu();

                }
                break;
        }
        return true;

    }
    /*
     * true:拦截孩子的事件，但会执行当前控件的onTouchEvent()方法
     * false:不拦截孩子的事件，事件继续传递
     * */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        boolean intercept=false;
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                //1.按下记录坐标
                downX=startX=event.getX();
                if(onStateChangListenter!=null){
                    onStateChangListenter.onDown(this);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                //2.记录结束时
                float endX=event.getX();
                float endY=event.getY();
                //3.计算偏移量
                float distanceX=endX-startX;


                startX=event.getX();
                startY=event.getY();

                //在X轴和Y轴移动的距离
                float DX=Math.abs(endX-downX);
                if (DX>8){
                    intercept=true;

                }

                break;
            case MotionEvent.ACTION_UP:
                break;

        }



        return intercept;

    }






    /*
    * //关闭Menu
    * */
    public void cloneMenu() {
        //--->0
        int distanceX=0-getScrollX();
        scroller.startScroll(getScrollX(),getScrollY(),distanceX,getScrollY());
        invalidate();//强制刷新

        if(onStateChangListenter!=null){
            onStateChangListenter.onClose(this);
        }

    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (scroller.computeScrollOffset()){
            scrollTo(scroller.getCurrX(),scroller.getCurrY());
            invalidate();
        }
    }
    /*
     * //打开Menu
     * */
    public void openMenu() {

        //--->menuWidth
        int distanceX=MeunWidth-getScrollX();
        scroller.startScroll(getScrollX(),getScrollY(),distanceX,getScrollY());
        invalidate();//强制刷新
        if(onStateChangListenter!=null){
            onStateChangListenter.onOpen(this);
        }
    }

    //监听SlideLayout状态的改变

    public interface OnStateChangListenter{

        void onClose(SlideLayout layout);
        void onDown(SlideLayout layout);
        void onOpen(SlideLayout layout);



    }

    private OnStateChangListenter onStateChangListenter;
    /*
    *
    * 设置SlideLayout状态的监听
    * */
    public  void setOnStateChangListenter(OnStateChangListenter onStateChangListenter){
        this.onStateChangListenter=onStateChangListenter;

    }
}
